import place from "../../../components/place.vue"
export default {
    name: "app",
    data() {
        let validatorTel = function (rule, value, callback) {
            if (!value) {
                return callback(new Error('电话不能为空！'))
            }
            let isTel = /^(0\d{2,3}-){0,1}\d{6,8}$/.test(value)
            let isMobile = /^1[34578]\d{9}$/.test(value)
            if (isTel || isMobile) {
                callback()
            } else {
                callback(new Error('请输入正确的电话！'))
            }
        }
        return {
            user_info: {
                userName: '',
                isUserManage: '',
                isGoodsManage: '',
                isInventoryManage: '',
            },
            update_info: {
                realName: '',
                address: '',
                phone: '',
            },
            place1: {
                display: 'block'
            },
            place2: {
                display: 'none'
            },
            personalRules: {
                phone: [
                    { validator: validatorTel, required: true, trigger: 'blur' },
                ]
            }
        }
    },
    components: {
        place,
    },
    created() {
        const that = this
        setTimeout(function () {
            that.get_current()
        }, 400);
    },
    // mounted: function () {
    //     this.get_current()
    // },
    methods: {
        get_current() {
            // console.log(this.$store.state.current)
            this.user_info.userName = this.$store.state.current.userName
            this.update_info.realName = this.$store.state.current.realName
            this.update_info.address = this.$store.state.current.address
            this.update_info.phone = this.$store.state.current.phone
            if (this.$store.state.current.isUserManage == true)
                this.user_info.isUserManage = "✔"
            else
                this.user_info.isUserManage = "✘"
            if (this.$store.state.current.isGoodsManage == true)
                this.user_info.isGoodsManage = "✔"
            else
                this.user_info.isGoodsManage = "✘"
            if (this.$store.state.current.isInventoryManage == true)
                this.user_info.isInventoryManage = "✔"
            else
                this.user_info.isInventoryManage = "✘"
        },
        modify_place1() {
            this.place1.display = 'none'
            this.place2.display = 'block'
        },
        modify_place2() {
            this.update_info.address = this.$store.state.address
            this.place1.display = 'block'
            this.place2.display = 'none'
        },
        async modify_information() {
            this.$refs.personalFormRef.validate(async valid => {
                // 验证不通过
                if (!valid) {
                    // 调用错误提示,重置表单信息
                    this.$store.commit("error", "信息不合法！请重新输入")
                    // this.resetLoginFrom(that)
                    return
                }
                else {
                    const update_info = this.$qs.parse(this.update_info)
                    const { data: res } = await this.$http.post('/gim/api/user/updateUser', update_info);
                    // console.log(res)
                    if (res.code == 1) {
                        this.$store.commit("success", res.response)
                        this.$store.dispatch('get_current')
                    }
                    else
                        this.$store.commit('error', res.response)
                }
            })
        },
    },
};